package com.sgb.kjwl.view.widget.manage


import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.LayoutAddViewBinding
import com.sgb.kjwl.model.entity.manage.AddItemEntity
import com.sgb.kjwl.view.ui.adapter.manage.AddViewAdapter
import com.swgk.core.util.MToast


class AddViewLayout : LinearLayout {
    private var mContext: Context? = null
    private lateinit var mBinging: LayoutAddViewBinding
    private lateinit var mAdapter: AddViewAdapter
    private var sortNum = 0 //排序 默认从1开始
    private lateinit var mLayoutManager : LinearLayoutManager
    private val reallyData  = ArrayList<AddItemEntity>()

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attributeSet: AttributeSet?) : this(context, attributeSet, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int)
            : super(context, attrs, defStyleAttr) {
        this.mContext = context!!
        init()
    }


    private fun init() {
        mBinging = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.layout_add_view, this, false)
        mLayoutManager = LinearLayoutManager(mContext, RecyclerView.VERTICAL, false)
        //mLayoutManager.stackFromEnd = true
        mBinging.rvSecondCategory.layoutManager = mLayoutManager
        mBinging.ivAdd.setOnClickListener {
            var size = mAdapter.data.size
            if (size == 5) {
                MToast.showToast(mContext, "最多五条")
                return@setOnClickListener
            }
            val addItemEntity = AddItemEntity()
            addItemEntity.sortNum = "".plus(++sortNum);
            mAdapter.arrayData.add(addItemEntity)
            mAdapter.notifyDataSetChanged()
            isShowOrHideIvAdd()
        }
        addView(mBinging.root)
        setShowAdd()
    }

    fun setHideAdd(){
        mBinging.llAdd.visibility = GONE
    }

    fun setShowAdd(){
        mBinging.llAdd.visibility = VISIBLE
    }

    public fun setAdapter(adapter: AddViewAdapter) {
        this.setAdapter(adapter, ArrayList())
    }

    public fun setAdapter(adapter: AddViewAdapter, data: ArrayList<AddItemEntity>) {
        this.mAdapter = adapter
        mBinging.rvSecondCategory.adapter = mAdapter
        mAdapter.setCallBack(object : AddViewAdapter.OnDeleteCallBack {
            override fun delete(position: Int) {
                mAdapter.arrayData.removeAt(position)
                isShowOrHideIvAdd()
                mAdapter.notifyDataSetChanged()
            }

            override fun onFocus(position: Int) {
                if (position == mAdapter.itemCount - 1){
                    mLayoutManager.scrollToPositionWithOffset(position, 300)
                }
            }
        })
        setData(data)
    }

    fun setData(data: List<AddItemEntity>) {
        reallyData.clear()
        if (data.isNotEmpty()) {
            for (entity in data) {
                sortNum = sortNum.coerceAtLeast(entity.getSortNum())
                if (!TextUtils.isEmpty(entity.fieldKey) || !TextUtils.isEmpty(entity.fieldValue)){
                    reallyData.add(entity)
                }
            }
        }
        mAdapter.data = reallyData
        isShowOrHideIvAdd()
    }

    private fun isShowOrHideIvAdd() {
        mBinging.ivAdd.visibility = if (mAdapter.data.size >= 5) GONE else VISIBLE

    }


    fun getDate(): List<AddItemEntity> {
        return mAdapter.arrayData
    }

}